from django.shortcuts import render
from goods.models import *
from django.views.generic import View
from django.core.cache import cache
from django_redis import get_redis_connection
from django.shortcuts import render,redirect
from django.urls import reverse
from order.models import OrderGoods
from django.core.paginator import Paginator
# Create your views here.
class index_view(View):
    def get(self,request):
        content=cache.get('indexpage')
        if not content:
            print("设置缓存")
            types = GoodsType.objects.all()
            advs=IndexPromotionBanner.objects.all().order_by('index')
            shows=IndexGoodsBanner.objects.all().order_by('index')
            for type in types:
                pic=IndexTypeGoodsBanner.objects.filter(type=type,display_type=1).order_by('index')
                word=IndexTypeGoodsBanner.objects.filter(type=type,display_type=0).order_by('index')
                type.pic=pic
                type.word=word
            content={
                'types':types,
                'advs':advs,
                'shows':shows,
            }
            cache.set('indexpage',content,3600)
        content.update(cart_num=0)
        con=get_redis_connection('default')
        user=request.user
        if user.is_authenticated:
            print(user.id)
            print('cart_{}'.format(user.id))
            num=con.hlen('cart_{}'.format(user.id))
            content.update(cart_num=num)
            print(num)
        return render(request,"index.html",content)
class detail_View(View):
    def get(self,request,id):
        try:
            good=GoodsSKU.objects.get(id=id)
        except GoodsSKU.DoesNotExist:
            return redirect(reverse("goods:index"))

        new_goods=GoodsSKU.objects.filter(type=good.type).order_by('-create_time')
        detail=good.goods.detail
        comments=OrderGoods.objects.filter(sku=good)
        same_goods=GoodsSKU.objects.filter(goods=good.goods).exclude(id=id)
        content={
            'good':good,
            'new_goods':new_goods,
            'detail':detail,
            'comments':comments,
            'same_goods':same_goods,
            
        }
        user=request.user
        
        if user.is_authenticated:
            con=get_redis_connection('default')
            history_id='history_{}'.format(request.user.id)
            con.lrem(history_id,0,id)
            con.lpush(history_id,id)
            print(user.id)
            print('cart_{}'.format(user.id))
            num=con.hlen('cart_{}'.format(user.id))
            content.update(cart_num=num)
            print(num)
            
        #with open("./test.html",'wb') as f:
        #    f.write(render(request,'detail.html',content).getvalue())
        return render(request,'detail.html',content)
class listView(View):
    def get(self,request,type_id,page):
        try:
            tp=GoodsType.objects.get(id=type_id)
        except GoodsType.DoesNotExist:
            return redirect(reverse("goods:index"))
        goods=GoodsSKU.objects.filter(type=tp)
        print(goods)
        sort_method=request.GET.get('sort')
        if sort_method  in ['id','sales','price']:
            goods=GoodsSKU.objects.filter(type=tp).order_by(sort_method)

        else:
            goods.order_by('id')
        print(goods)
        try :
            page=int(page)
        except:
            page=1
        goods=Paginator(goods,2).page(page)
        types=GoodsType.objects.all()
        content={
            'goods':goods,
            'tp':tp,
            'types':types,
            'sort_method':sort_method,
        }
        new_goods=GoodsSKU.objects.filter(type=tp).order_by('-create_time')[0:2]
        content.update(new_goods=new_goods)
        user=request.user
        if user.is_authenticated:
             con=get_redis_connection('default')
             num=con.hlen('cart_{}'.format(user.id))
             content.update(cart_num=num)
        return render(request,'list.html',content)
